package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.iap.IapGiftPush;
import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameTest.*;
import com.motu.monstercity.server.game.base.AllParam;
import com.motu.monstercity.server.game.base.CommonUtils;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.act.ActConfig;
import com.motu.monstercity.server.game.commondata.act.ActDay;
import com.motu.monstercity.server.game.commondata.act.ActRechargeTask;
import com.motu.monstercity.server.game.commondata.bag.ItemId;
import com.motu.monstercity.server.game.commondata.partner.PartnerMain;
import com.motu.monstercity.server.game.commondata.system.SystemConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.*;
import com.motu.vertx.module.utility.base.ServerConfig;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

public class TestHandler {
	//CMD_GAME_TEST_SERVER = 70101; // 服务端临时测试专用
	public static Object CMD_GAME_TEST_SERVER(RequestParam clientParam) throws Exception {
		CSGameTestServerRequest request = CSGameTestServerRequest.parseFrom(clientParam.getData());
		CSGameTestServerResponse.Builder response = CSGameTestServerResponse.newBuilder();
		long userId = clientParam.getUserId();
		if (ServerConfig.isOnlineServer()) {// 非正式服才允许执行
			return ErrorWord.WRONG_STATUS;
		}

		PBUserData.Builder pbUserData = response.getUserBuilder();
		UserInfo userInfo = GameUser.getUserInfo(userId);
		UserExtInfo userExtInfo = GameUser.getUserExtInfo(userId);
//		int param = Integer.parseInt(request.getParam());
//		int totalLevel = FightManager.takeFightSnatchMoneyNum(param);
//
//		UserHonor userHonor = MainTaskManager.getUserHonor(userId, 101);
//		System.out.println(userHonor.getNum() + " reward:" + userHonor.getGetReward());
//		RankActManager.testMatchCrossRankAct();
//		GameUser.sendRedPointPrompt(userInfo, userExtInfo, SystemConstant.RED_POINT_TYPE_INIT, userId);
//		GameDataManager.initParams();
//		UnionScuffleManager.closeMatchRank();
//		GameUser.sendRedPointPrompt(userInfo, userExtInfo, SystemConstant.RED_POINT_TYPE_INIT);
//		UserUnionScuffleInfoWeek userUnionScuffleInfoWeek = UnionScuffleManager.getUserUnionScuffleInfo(userId);
//		userUnionScuffleInfoWeek.putWeekResetTime(TimeUtils.getCurTime() - TimeUtils.WEEK);
//		userUnionScuffleInfoWeek.update();
//		UnionScuffleManager.clearRankData();
//		UserUnion userUnion = UnionManager.getUserUnion(userId);
//		UnionInfo unionInfo = UnionManager.getUnionInfo(userUnion.getUnionId());
//		int boxId = UnionManager.getUnionBoxCurrentBoxId(unionInfo.getLevel());
//		System.out.println("boxId = " + boxId);
//		RankActManager.matchCrossRankAct();
		ActConfig actConfig = RankActManager.getActConfig(40013);
		UserCrossRechargeActTask userCrossRechargeActTask = RankActManager.getUserCrossRechargeActTask(userId, actConfig.getConfigId(), ActRechargeTask.TASK_TYPE_VIP_SCORE_DAILY);
//		userCrossRechargeActTask.putCurP(11);
		RankActManager.checkCrossRankRechargeTaskRedPoint(userInfo, actConfig, userCrossRechargeActTask, 2, 0);
		return response;
	}

	public static Object CMD_GAME_TEST_ADD_ITEM(RequestParam clientParam) throws Exception {
		CSGameTestAddItemRequest request = CSGameTestAddItemRequest.parseFrom(clientParam.getData());
		CSGameTestAddItemResponse.Builder response = CSGameTestAddItemResponse.newBuilder();

		if (ServerConfig.isOnlineServer()) {// 非正式服才允许执行
			return ErrorWord.WRONG_STATUS;
		}


		int count = request.getNum();
		int itemId = request.getItemId();
		PBUserData.Builder pbUserData = response.getUserBuilder();
		long userId = clientParam.getUserId();
		UserInfo userInfo = GameUser.getUserInfo(userId);
		RewardItem rewardItem = new RewardItem(itemId, count);
		RewardManager.addReward(userInfo, rewardItem, pbUserData, LogType.GM_SEND);
		return response;
	}

	public static Object CMD_GAME_TEST_CREATE_CHILD(RequestParam clientParam) throws Exception {
		CSGameTestCreateChildRequest request = CSGameTestCreateChildRequest.parseFrom(clientParam.getData());
		CSGameTestCreateChildResponse.Builder response = CSGameTestCreateChildResponse.newBuilder();
		if (ServerConfig.isOnlineServer()) {// 非正式服才允许执行
			return ErrorWord.WRONG_STATUS;
		}

		PBUserData.Builder pbUserData = response.getUserBuilder();

		return response;
	}


	// CMD_GAME_TEST_WGX = 70107; // wgx测试用的接口
	public static Object CMD_GAME_TEST_WGX(RequestParam clientParam) throws Exception {
		CSGameTestWgxRequest request = CSGameTestWgxRequest.parseFrom(clientParam.getData());
		CSGameTestWgxResponse.Builder response = CSGameTestWgxResponse.newBuilder();
		long userId = clientParam.getUserId();
		if (ServerConfig.isOnlineServer()) {// 非正式服才允许执行
			return ErrorWord.WRONG_STATUS;
		}

		int itemId = request.getItemId();
		UserInfo userInfo = GameUser.getUserInfo(userId);
		UserStage userStage = FightManager.getUserStage(userId);
		UserPower userPower = GameUser.getUserPower(userId);
		UserAffair userAffair = GameUser.getUserAffair(userId);

		PBUserData.Builder pbUserData = response.getUserBuilder();
//		RewardManager.addReward(userInfo, 100042, itemId, pbUserData, LogType.GM_SEND);

//		String sql = "select * from user_achieve where user_id=10000000000189;";
//		JsonArray jsonArray = Future.await(MysqlTool.queryJsonArrayFromDb(MysqlClient.getGamePool(), sql, new JsonArray()));
//		for (int i=0;i<jsonArray.size();i++) {
//			JsonObject jsonObject = jsonArray.getJsonObject(i);
//			long uid = jsonObject.getLong("user_id");
//			int taskType = jsonObject.getInteger("task_id");
//			long num = jsonObject.getLong("num");
//			System.out.println("uid:"+uid+" taskType:"+taskType+" num:"+num);
//			MainTaskManager.updateUserTaskValue(pbUserData, uid, taskType, num, 0, false);
//		}
//		RewardManager.addReward(userInfo, request.getItemId(), 1, pbUserData, LogType.RESTAURANT_ROOM_UPGRADE);
//		long uniqKey = FeastManager.getFeastUniqKey(userId, itemId);
//		System.out.println("uniqKey:"+uniqKey);

//		Stage stage = userStage.takeCompleteStage();
//		//RewardManager.addReward(userInfo, 603999, 1, pbUserData, LogType.BUILD_HOSPITAL_RECOVER_TROOPS);
//		//long offGoldNum = BuildManager.countTotalEarnNow(pbUserData, userInfo, userPower, userAffair, true);
//		EntityManager.delEntity(ServerFeast.class);
//		EntityManager.delEntity(ServerFeastSeat.class);

//		CaveMain caveMain = CaveManager.getCaveMain(2061);
//		CaveManager.updateCaveRank(userInfo.getServerId(), userId, caveMain);

//		int koNum = Tool.getRandom(11, 20);
//		long enemyId = userId + koNum;
//		FightManager.createSnatchFame(userInfo, enemyId, koNum);// 生成名人广播数据

//		WorldEvent 	worldEvent = WorldManager.getWorldEvent(1);
//		for (int i=0;i<100;i++) {
//			int aa = WorldManager.randomDistance(worldEvent);
//			System.out.println("aaa:" + aa);
//		}
//		UserWorldInfo userWorldInfo = WorldManager.getUserWorldInfo(userId);
//		boolean flag = WorldManager.checkIsNeedRefreshEvent(userWorldInfo.getEventRefreshTime());
//		System.out.println("flag:" + flag);

//		UserMonsterTideInfo userMonsterTideInfo = FightManager.unlockMonsterTide(userId);
//		ProtoDataUtils.updatePBUserData(pbUserData, userMonsterTideInfo, true);
//		int time = WorldManager.getOneWayTime(request.getItemId());
//		SkillManager.updateSkillEffect(userId, 910510, request.getItemId());
//		Skill skill = SkillManager.getSkill(920000);
//		int level = request.getItemId();
//		//System.out.println("level:" + level + " result:" + skill.countTotalAdd(level));
//		String ss = "0";
//		List<RewardItem> rewardItems = CommonUtils.takeReawrdItemFromStr(ss);
//		System.out.println("xxx:" + rewardItems.size());

//		UserPartner userPartner = PartnerManager.getUserPartner(userId, 502420);
//		List<UserSecretary> secretaryList = SecretaryManager.getUserSecretaries(userId);
//		GameUser.initPartnerPower(userInfo, userPower, userPartner, secretaryList);
//		userPartner.update();
//		int n = request.getItemId();
//		long aa = WorldManager.getOneWayTime(n);
//		System.out.println("aa:" + aa);
//		UserSecretary userSecretary = SecretaryManager.getUserSecretary(userId, 540214);
//		userSecretary.reCountBusiness(userInfo, userPower);
//		userSecretary.update();
		//RewardManager.addReward(userInfo, ItemId.PERSON, request.getItemId(), pbUserData, LogType.GM_SEND);
		//userPower.updateEarnAddMonster(pbUserData, userInfo, AllParam.MONSTER_TIDE_BUFFER[2]);// 增加永久加成
//		String aa = "61540003;1";
//		String bb = "61540003;10";
//		String cc = "61540001;10";
//		List<RewardItem> rewardItemList = CommonUtils.takeReawrdItemFromStr(aa);// 普通奖励
//		System.out.println("aa:");
//		CommonUtils.printRewwardItemList(rewardItemList);
//
//		CommonUtils.mergeItemList(rewardItemList, CommonUtils.takeReawrdItemFromStr(bb));
//		System.out.println("bb:");
//		CommonUtils.printRewwardItemList(rewardItemList);
//
//
//		CommonUtils.mergeItemList(rewardItemList, CommonUtils.takeReawrdItemFromStr(cc));
//		System.out.println("cc:");
//		CommonUtils.printRewwardItemList(rewardItemList);
//		UserWorldInfo userWorldInfo = WorldManager.getUserWorldInfo(userId);
//		WorldManager.creatUrgentEvent(pbUserData, userInfo, userWorldInfo);
//		userWorldInfo.update();
		//WorldManager.creatUrgentEvent(pbUserData, userInfo, userWorldInfo);
		UserPartner userPartner = PartnerManager.getUserPartner(userId, request.getItemId());
		ProtoDataUtils.updatePBUserData(pbUserData, userPartner);


		return response;
	}

	// CMD_GAME_TEST_HWP = 70108; // hwp测试用的接口
	public static Object CMD_GAME_TEST_HWP (RequestParam clientParam) throws Exception {
		CSGameTestHwpRequest request = CSGameTestHwpRequest.parseFrom(clientParam.getData());
		CSGameTestHwpResponse.Builder response = CSGameTestHwpResponse.newBuilder();
		long userId = clientParam.getUserId();
		UserInfo userInfo = GameUser.getUserInfo(userId);
		if (ServerConfig.isOnlineServer()) {// 非正式服才允许执行
			return ErrorWord.WRONG_STATUS;
		}
		PBUserData.Builder pbUserData = response.getUserBuilder();
//		UserInfo userInfo = GameUser.getUserInfo(userId);
//		Act act = ActManager.getActByType(ActConstant.ACT_TYPE_1001, userInfo.getServerId());
//		ActConventionManager.closeActConventionRankReward(act);
//		UserDailyRaffleDraw userDailyRaffleDraw = ActManager.getUserDailyRaffleDraw(userId);
//		userDailyRaffleDraw.extraReward(userInfo, pbUserData);
		int[] itemIds = {
				61502110, 61502120, 61502130, 61502140, 61502150,
				61520001, 61520002, 61520003, 61520101, 61520102,
				61540001, 61540003, 61540005, 61540102, 61540103
		};
		List<RewardItem> rewardItems = new ArrayList<>();
		for (int itemId : itemIds) {
			rewardItems.add(new RewardItem(itemId, 100));
		}
		RewardManager.addReward(userInfo, rewardItems, pbUserData, LogType.GM_SEND);
		return response;
	}

	// CMD_GAME_TEST_RED_POINT = 70109; // 测试红点
	public static Object CMD_GAME_TEST_RED_POINT (RequestParam clientParam) throws Exception {
		CSGameTestRedPointRequest request = CSGameTestRedPointRequest.parseFrom(clientParam.getData());
		CSGameTestRedPointResponse.Builder response = CSGameTestRedPointResponse.newBuilder();
		long userId = clientParam.getUserId();
		UserInfo userInfo = GameUser.getUserInfo(userId);
		UserExtInfo userExtInfo = GameUser.getUserExtInfo(userId);
		int promptType = request.getPromptType();
		String param = request.getParam();
		if (!Tool.isEmpty(param)) {
			JsonArray paramArray = new JsonArray(param);
			Object[] params = paramArray.getList().toArray(new Object[0]);
			GameUser.sendRedPointPrompt(userInfo, userExtInfo, promptType, params);
		} else {
			GameUser.sendRedPointPrompt(userInfo, userExtInfo, promptType);
		}
		return response;
	}


	// CMD_GAME_TEST_ALIPAY = 70110; // 生成支付宝支付参数用
	public static Object CMD_GAME_TEST_ALIPAY (RequestParam clientParam) throws Exception {
		CSGameTestAlipayRequest request = CSGameTestAlipayRequest.parseFrom(clientParam.getData());
		CSGameTestAlipayResponse.Builder response = CSGameTestAlipayResponse.newBuilder();
		long userId = clientParam.getUserId();

		String string = "";// 原来的配置
		JsonObject result = new JsonObject(string);

		JsonObject webObj = new JsonObject();// 网页充值的支付宝配置
		String webPriKey = "";// 私钥 web应用配置的私钥，公钥需配置在支付宝的后台
		webObj.put("appid", "2021005166634172");
		webObj.put("privatekey", webPriKey);
		webObj.put("returnurl", "https://xzlrmall.more2.cn/#/web_charge/pay?paytype=2");
		webObj.put("returnurl2", "https://xzlrmall.more2.cn/#/applink");
		result.put("web", webObj);


		JsonObject appObj = new JsonObject();// 游戏内支付宝应用的配置
		String appPriKey = "";// 私钥 游戏应用配置的私钥，公钥需配置在支付宝的后台
		appObj.put("appid", "2021005165648258");
		appObj.put("privatekey", appPriKey);
		appObj.put("returnurl", "");
		result.put("app", appObj);

		System.out.println(result.toString());

		return response;
	}

}
